package com.guanyong.services;

import com.guanyong.dao.ISubjectDaoMapper;
import com.guanyong.model.SubjectModel;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by mxk on 2017/9/22.
 */

@Service
public class SubjectService {

    @Autowired
    ISubjectDaoMapper iSubjectDaoMapper;


    public SubjectModel GetSubjectByIdModel(String fid)
    {
        return  iSubjectDaoMapper.selectByPrimaryKey(fid);
    }


    public List<SubjectModel> GetSubjectAllModel()
    {
        return  iSubjectDaoMapper.selectAllSubjectList();
    }

    public List<SubjectModel> selectViodeSubjectList(String fViode)
    {
        return  iSubjectDaoMapper.selectViodeSubjectList(fViode);
    }


    public List<SubjectModel> GetSubjectByTypeModel(Integer type)
    {
        return  iSubjectDaoMapper.selectAllSubjectByType(type);
    }

    public ResultObject<Integer> AddSubject(SubjectModel subjectModel)
    {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=iSubjectDaoMapper.AddSubject(subjectModel);
        if (res>-1 )
        {
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }

    public ResultObject<Integer> UpdateSubject(SubjectModel subjectModel)
    {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=iSubjectDaoMapper.UpdateSubject(subjectModel);
        if (res>-1 )
        {
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }


    public ResultObject<Integer> DeleteSubject(Integer id)
    {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=iSubjectDaoMapper.DeleteSubject(id);
        if (res>-1 )
        {
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }

    public ResultObject<Integer> DeleteSubjectVideo(Integer subject_id, String videoid) {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=iSubjectDaoMapper.DeleteSubjectVideo(subject_id,videoid);
        if (res>-1 )
        {
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }

    public List<SubjectModel> GetSubjectVideoAllModel(String videoid) {
        List<SubjectModel> ViodeSubjectList =iSubjectDaoMapper.selectViodeSubjectList(videoid);
        Set<Integer> ViodeSubjectSet= new HashSet<Integer>();
        for (SubjectModel subjectModel:ViodeSubjectList){
            ViodeSubjectSet.add(subjectModel.getfId());
        }
        List<SubjectModel> subjectList=iSubjectDaoMapper.selectAllSubjectList();
        for (SubjectModel subjectModel:subjectList){
            if (ViodeSubjectSet.contains(subjectModel.getfId())){
                subjectModel.setfCheck(1);
            }else {
                subjectModel.setfCheck(0);
            }
        }
        return subjectList;
    }
    @Transactional
    public ResultObject<Integer> updatesubjectvideo(String videoid,String[] subjectId) {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res1 = iSubjectDaoMapper.DeleteToSubjectVideo(videoid);
        Integer res2=1;
        if (res1>-1){
            if (subjectId!=null&&subjectId.length>0){
                for (String subjectVideo:subjectId){
                    int temp = iSubjectDaoMapper.AddSubjectVideo(videoid,Integer.valueOf(subjectVideo));
                    if (temp==-1){
                        res2=-1;
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        break;
                    }
                }
            }
        }else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        if (res1>-1&&res2>-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }
}
